package com.qwe.graduation.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qwe.graduation.entity.vo.MessageLeaveVo;
import com.qwe.graduation.entity.vo.VideoLeaveVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.qwe.graduation.utils.exception.Result;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import com.qwe.graduation.service.IVideoLeaveService;
import com.qwe.graduation.entity.VideoLeave;


/**
 * <p>
 * 视频留言表 前端控制器
 * </p>
 *
 * @author 林卓锋
 * @since 2023-02-02
 */
@RestController
@RequestMapping("/videoLeave")
public class VideoLeaveController {

    @Resource
    private IVideoLeaveService videoLeaveService;

    //新增或更改
    @PostMapping
    public Result save(@RequestBody VideoLeave videoLeave) {
        return Result.success(videoLeaveService.saveOrUpdate(videoLeave));
    }

    //删除
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id) {
        return Result.success(videoLeaveService.removeById(id));
    }

    //查找全部
    @GetMapping
    public List<VideoLeave> findAll() {
        return videoLeaveService.list();
    }

    //根据id查询
    @GetMapping("/{id}")
    public List<VideoLeave> findOne(@PathVariable String id) {
        return videoLeaveService.list();
    }

    //批量删除
    @PostMapping("/delete/batch")
    public Result deleteBeatch(@RequestBody List<String> ids) {
        return Result.success(videoLeaveService.removeByIds(ids));
    }

    //分页
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "",required = false) String name,
                           @RequestParam(defaultValue = "",required = false) String headline,
                           @RequestParam(defaultValue = "",required = false) String module) {
        Page<VideoLeaveVo> page = new Page<>(pageNum,pageSize);
        List<VideoLeaveVo> list = videoLeaveService.findList(name,headline,module);
        int start = (int)((page.getCurrent() - 1) * page.getSize());
        // 当前页最后一条数据在List中的位置
        int end = (int)((start + page.getSize()) > list.size() ? list.size() : (page.getSize() * page.getCurrent()));
        page.setRecords(new ArrayList<>());
        page.setTotal(list.size());
        if (page.getSize()*(page.getCurrent()-1) <= page.getTotal()) {
            // 分隔列表 当前页存在数据时 设置
            page.setRecords(list.subList(start, end));
        }
        return Result.success(page);
    }

}

